//
//  UINavigationController+Extension.swift
//  GeekTime
//
//  Created by monkey on 2020/1/30.
//  Copyright © 2020 itcast. All rights reserved.
//

import UIKit

extension UINavigationController: XGSelfAware
{
    static func awake()
    {
        DispatchQueue.once {
            // 交换push方法
            guard let push = class_getInstanceMethod(self, #selector(pushViewController(_:animated:))),
                let xg_push = class_getInstanceMethod(self, #selector(xg_pushViewController(_:animated:))) else {
                    return
            }

            method_exchangeImplementations(push, xg_push)
        }
    }
    
    @objc private func xg_pushViewController(_ viewController: UIViewController, animated: Bool)
    {
        // 添加手势对象
        if interactivePopGestureRecognizer?.view?.gestureRecognizers?.contains(panGestureRecognizer) != true {
            if let targets = (interactivePopGestureRecognizer?.value(forKey: "targets")) as? [AnyObject],
                let target = targets.first?.value(forKey: "target") {
                panGestureRecognizer.addTarget(target, action: NSSelectorFromString("handleNavigationTransition:"))
                panGestureRecognizer.delegate = panGestureRecognizerDelegate
                interactivePopGestureRecognizer?.view?.addGestureRecognizer(panGestureRecognizer)
                // 禁用系统手势识别器
                interactivePopGestureRecognizer?.isEnabled = false
            }
        }
                
        
        if self.viewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
//            view.backgroundColor = UIColor.white
            view.theme_backgroundColor = "colors.cell_background"
        }
        
        xg_pushViewController(viewController, animated: animated)
    }
    
    // 关联对象key要唯一 不能直接使用"" 因为string是值类型 会进行拷贝 不唯一
    private struct AssociateKeys {
        static var panGestureRecognizer = "_panGestureRecognizer"
        static var panGestureRecognizerDelegate = "_panGestureRecognizerDelegate"
    }
        
    /// 左滑手势器
    var panGestureRecognizer: UIPanGestureRecognizer {
        var panGestureRecognizer = objc_getAssociatedObject(self, &AssociateKeys.panGestureRecognizer)
        if panGestureRecognizer == nil {
            panGestureRecognizer = UIPanGestureRecognizer()
            objc_setAssociatedObject(self, &AssociateKeys.panGestureRecognizer, panGestureRecognizer, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        
        return panGestureRecognizer as! UIPanGestureRecognizer
    }
    
    private var panGestureRecognizerDelegate: XGPanGestureRecognizerDelegate {
        var panGestureRecognizerDelegate: XGPanGestureRecognizerDelegate? = objc_getAssociatedObject(self, &AssociateKeys.panGestureRecognizerDelegate) as? XGPanGestureRecognizerDelegate
        if panGestureRecognizerDelegate == nil {
            panGestureRecognizerDelegate = XGPanGestureRecognizerDelegate(navigationController: self)
            objc_setAssociatedObject(self, &AssociateKeys.panGestureRecognizerDelegate, panGestureRecognizerDelegate, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        
        return panGestureRecognizerDelegate!
    }
}

// MARK: - 手势代理对象

private class XGPanGestureRecognizerDelegate: NSObject,UIGestureRecognizerDelegate
{
    private weak var navigationController: UINavigationController?
    
    init(navigationController: UINavigationController)
    {
        self.navigationController = navigationController
        
        super.init()
    }
    
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool
    {
        // 是否正在进行转场动画
        let isTransitioning = navigationController?.value(forKey: "isTransitioning") as? Bool
        let pan = gestureRecognizer as! UIPanGestureRecognizer
        if (navigationController?.viewControllers.count ?? 0) == 1 {
            return false
        } else if pan.translation(in: pan.view).x <= 0 {
            return false
        } else if isTransitioning == true {
            return false
        } else {
            return true
        }
    }
}
